package cn.wolfcode.wolf2w.service.impl;


import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {


    //在这里引用后期后台项目引用会导致报错
   /* @Value("${sms.url}")
    private String smsUrl;
    @Value("${sms.Key")
    private String appKey;*/

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private StrategyCatalogRepository repository;
    @Autowired
    private MongoTemplate template;

    @Override
    public void save(StrategyCatalog StrategyCatalog) {
        repository.save(StrategyCatalog);
    }



    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        StrategyCatalog StrategyCatalog = repository.findById(id).get();
        return StrategyCatalog;
    }

    @Override
    public List<StrategyCatalog> list() {
        List<StrategyCatalog> StrategyCatalogs = repository.findAll();
        return StrategyCatalogs;
    }

    //分页查询
    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        Query query=new Query();
        //repository只能针对所有数据进行分页
        //template不能直接对数据进行分页
        //使用template间接分页
        //pageResult 核心字段
        //totalCount 总记录数
        long totalcount = template.count(query, StrategyCatalog.class);
        if (totalcount==0){
            //空的分页对象
            return Page.empty();
        }
        //list 当前页显示数据集合
        Pageable pageable= PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(), Sort.Direction.ASC,"_id");
        query.with(pageable);
        List<StrategyCatalog> list = template.find(query, StrategyCatalog.class);
        return new PageImpl<>(list,pageable,totalcount);
    }

    @Override
    public List<CatalogVO> groupList() {
        List<CatalogVO> list=new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class, Aggregation.group("destName").push("name").as("names").push("id").as("ids"));
        AggregationResults<Map> result = template.aggregate(agg, Map.class);
        List<Map> dates = result.getMappedResults();
        for (Map data:dates){
            CatalogVO vo=new CatalogVO();
            vo.setDestName(data.get("_id").toString());
            List<Object> names= (List<Object>)data.get("names");
            List<Object> ids = (List<Object>)data.get("ids");
            List<StrategyCatalog> catalogList=new ArrayList<>();
            for (int i = 0;i<names.size();i++){
                StrategyCatalog sc = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                sc.setId(id);
                sc.setName(name);
                catalogList.add(sc);
            }
            vo.setCatalog(catalogList);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String destId) {
        List<StrategyCatalog> list=repository.findByDestId(destId);
        for (StrategyCatalog strategyCatalog:list){
            List<Strategy> sts=strategyService.queryCatalogId(strategyCatalog.getId());
            strategyCatalog.setStrategies(sts);
        }
        return list;
    }


}
